package com.toommi.refuse.controller;

import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.Suggestion;
import com.toommi.refuse.service.SuggestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * ..
 * 建议与反馈
 *
 * @author yangg
 * @since 2019-07-25 17:27
 */
@RestController
@RequestMapping(value = "/api")
@Slf4j
@AllArgsConstructor
@Api(tags = "建议与回复")
public class SuggestionController {

    private SuggestionService suggestionService;

    /**
     * ..
     * 新增建议反馈、及回复
     *
     * @param suggestion 建议信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增建议/回复", notes = "新增建议/回复")
    @ApiImplicitParam(name = "suggestion", value = "建议/回复详情实体", required = true, dataType = "Suggestion")
    @RequestMapping(value = "/suggestion", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public CommonResponse addSuggestion(@RequestBody Suggestion suggestion) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            suggestionService.addSuggestion(suggestion);
        } catch (Exception e) {
            log.error("add suggestion error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 分页获取建议信息
     *
     * @param pageNum      页码
     * @param pageSize     每页数据量
     * @param keyWord      模糊查询关键字
     * @param type         类型
     * @param wechatUserId 用户id
     * @return 建议列表信息
     */
    @ApiOperation(value = "查询大件垃圾分类", notes = "查询大件垃圾分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "keyWord", value = "查询关键字", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型，1为建议，2为回复", required = true, dataType = "int"),
            @ApiImplicitParam(name = "wechatUserId", value = "微信用户id", required = true, dataType = "Long")
    })
    @RequestMapping(value = "/suggestions", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse getSuggestions(int pageNum, int pageSize, String keyWord, int type, long wechatUserId) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(suggestionService.findSuggestions(pageNum, pageSize, keyWord, type, wechatUserId));
        } catch (Exception e) {
            log.error("find suggestions error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 查询建议详情
     *
     * @param id 建议id
     * @return 建议详情
     */
    @ApiOperation(value = "查询建议详情", notes = "查询建议详情")
    @ApiImplicitParam(name = "id", value = "建议id", required = true, dataType = "Long")
    @RequestMapping(value = "/suggestion/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse getSuggestion(@PathVariable(name = "id") int id) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(suggestionService.findSuggetionDetail(id));
        } catch (Exception e) {
            log.error("get suggestion detail error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
}
